module UsersHelper

  def projects(user)
    projects = (user.kind_of?(User)) ? user.projects : user;
    projects = projects.map do |project|
      link_to(project.title, project_path(:id => project.id, :name => project.name));
    end
    (projects.empty?) ? "Nenhum" : projects.to_sentence(:connector => "e");
  end
  
  def projects_list(projects)
    return("Nenhum") if projects.empty?;
    list = "<ul>\n";
    projects.each do |project|
      link = link_to(project.title, project_path(:id => project.id, :name => project.name));
      klass = (project.user?(user)) ? " class=\"my-project\"" : "";
      list << "<li#{klass}>#{link}</li>\n";
    end
    list + "</ul>";
  end
  
  def projects_same(_user, ref_user=user, more=true)
    _projects = _user.projects.select{|project| project.known_by?(ref_user);}
    projects_from = (more) ? projects_same(ref_user, _user, false) : [];
    all_projects = (_projects + projects_from).sort{|a, b| a.title <=> b.title};
    (more) ? projects_list(all_projects) : _projects;
  end
  
end